package com.sunnypower.rushfitapp;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.sunnypower.rushfitapp.BodyDataPart.BodyDataActivity;
import com.sunnypower.rushfitapp.Orderpart.OrderListAdapter;
import com.sunnypower.rushfitapp.entity.User;
import com.sunnypower.rushfitapp.entity.VOrderCourseQuery;
import com.sunnypower.rushfitapp.util.GetData;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class UserMainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_mainvip;
    private Button btn_mainuser;
    private Button btn_vipinfo;
    private Button btn_bodydata;
    private Button btn_history;
    private ImageView img_user;
    private TextView tv_username;
    private TextView tv_location;
    private TextView tv_slogan;
    private String uid;
    private String username;
    private User user;
    private String sex;
    private static final String TAG = "UserMainActivity";

    private List<VOrderCourseQuery> mOrderBeanList;
    private OrderListAdapter adapter;
    private ListView lv_order;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rushfit_user);
        try {
            init();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        mOrderBeanList = new ArrayList<>();
        try {
            initOrderData();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        lv_order = findViewById(R.id.lv_order);
        adapter = new OrderListAdapter(UserMainActivity.this, mOrderBeanList);
        lv_order.setAdapter(adapter);

    }

    private void init() throws IOException {
        //获取主页面传过来的username值
        Intent intent = getIntent();
        uid = intent.getStringExtra("uid");
        System.out.println(uid);
        username = intent.getStringExtra("username");
        System.out.println(username);


        btn_mainvip = (Button) findViewById(R.id.btn_mainvip);
        btn_mainuser = (Button) findViewById(R.id.btn_mainuser);
        btn_vipinfo = (Button) findViewById(R.id.btn_vipinfo);
        btn_bodydata = (Button) findViewById(R.id.btn_bodydata);
        btn_history = (Button) findViewById(R.id.btn_history);

        btn_vipinfo = findViewById(R.id.btn_vipinfo);
        img_user = findViewById(R.id.img_user);
        tv_username = findViewById(R.id.tv_username);
        tv_location = findViewById(R.id.tv_location);
        tv_slogan = findViewById(R.id.tv_slogan);

        String data = GetData.doGet("http://120.24.24.141:10990/rushfit/getuserinfo2app?username=" + username);
        System.out.println(data);
        parseJsonWithJsonObject(data);
        byte[] decodedString = Base64.decode(user.getPhoto().split(",")[1], Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        img_user.setImageBitmap(decodedByte);
        tv_username.setText(user.getName());
        tv_location.setText(user.getAddr());
        if (user.getSex() == 1) {
            sex = "男";
        } else {
            sex = "女";
        }
        tv_slogan.setText(sex + " | " + user.getTel());
        tv_username.setSelected(true);
        tv_location.setSelected(true);
        tv_slogan.setSelected(true);
        //设置按钮的点击事件
        btn_mainvip.setOnClickListener(this);
        btn_mainuser.setOnClickListener(this);
        btn_vipinfo.setOnClickListener(this);
        img_user.setOnClickListener(this);
        btn_bodydata.setOnClickListener(this);
        btn_history.setOnClickListener(this);
    }

    private void initOrderData() throws JSONException {
        String data = GetData.doGet("http://120.24.24.141:10990/rushfit/FindFitOrderCourse?uphone=" + username);
        System.out.println(data);
        //解析json数据
        System.out.println("responseData：" + data);

        JSONArray OrderList = null;

        try {
            JSONObject jsonObject = new JSONObject(data);
            JSONObject jsonObject2 = jsonObject.getJSONObject("OrderCourseList");
            OrderList = jsonObject2.getJSONArray("list");
            System.out.println("CourseList：" + OrderList);
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        for (int i = 0; i < OrderList.length(); i++) {
            JSONObject jsoncourse = OrderList.getJSONObject(i);
            VOrderCourseQuery bean = new VOrderCourseQuery();
            bean.setCname(jsoncourse.getString("cname"));
            bean.setTname(jsoncourse.getString("tname"));
            bean.setRCtime(jsoncourse.getString("rctime"));
            mOrderBeanList.add(bean);
        }
    }

    @Override
    public void onClick(View v) {
        Toast toast;
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_mainvip:
                toast = onCardClick(getApplicationContext(), "欢迎来到会员服务", Toast.LENGTH_SHORT);
                toast.show();
                intent = new Intent(UserMainActivity.this, VipMainActivity.class);
                intent.putExtra("uid", uid);
                intent.putExtra("username", username);
                //resultCode  1代表成功，0代表失败：
                setResult(1, intent);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_mainuser:
                toast = onCardClick(getApplicationContext(), "已在当前页面", Toast.LENGTH_SHORT);
                toast.show();
                break;
            case R.id.btn_vipinfo:
                toast = onCardClick(getApplicationContext(), "欢迎来到会员验证", Toast.LENGTH_SHORT);
                toast.show();
                intent = new Intent(UserMainActivity.this, VipCheckActivity.class);
                intent.putExtra("uid", uid);
                intent.putExtra("tel", user.getTel());
                intent.putExtra("name", user.getName());
                intent.putExtra("time", user.getTime().toString());
                intent.putExtra("state", user.getState());
                //resultCode  1代表成功，0代表失败：
                setResult(1, intent);
                startActivity(intent);
                break;
            case R.id.img_user:
                intent = new Intent(UserMainActivity.this, UserEditActivity.class);
                intent.putExtra("uid", uid);
                intent.putExtra("username", username);
                intent.putExtra("photo", user.getPhoto());
                intent.putExtra("name", user.getName());
                intent.putExtra("sex", user.getSex());
                intent.putExtra("addr", user.getAddr());
                //resultCode  1代表成功，0代表失败：
                setResult(1, intent);
                startActivity(intent);
                finish();
                break;
            case R.id.btn_bodydata:
                intent = new Intent(UserMainActivity.this, BodyDataActivity.class);
                intent.putExtra("sex", user.getSex());
                intent.putExtra("uid", uid);
                Log.d(TAG, "onClick: uid============"+uid);
                setResult(1, intent);
                startActivity(intent);
                break;
            case R.id.btn_history:
                intent = new Intent(UserMainActivity.this, CourseHistoryActivity.class);
                intent.putExtra("uid", uid);
                setResult(1, intent);
                startActivity(intent);
                break;
        }
    }

    public Toast onCardClick(Context context, CharSequence text, int duration) {
        Toast toast = Toast.makeText(context, text, duration);
        toast.setGravity(Gravity.CENTER | Gravity.CENTER, 0, 0);
        return toast;
    }

    //解析json数据
    private void parseJsonWithJsonObject(String data) throws IOException {
        System.out.println("responseData：" + data);
        try {
            JSONObject jsonObject = new JSONObject(data);
            System.out.println("jsonObject：" + jsonObject);
            String sex = jsonObject.getString("sex");
            String name = jsonObject.getString("name");
            String tel = jsonObject.getString("tel");
            String addr = jsonObject.getString("addr");
            String state = jsonObject.getString("state");
            String photo = jsonObject.getString("photo");
            String time = jsonObject.getString("time");
            System.out.println(time);
            user = new User();
            user.setAddr(addr);
            user.setName(name);
            user.setPhoto(photo);
            user.setSex(Integer.valueOf(sex));
            user.setState(Integer.valueOf(state));
            user.setTel(tel);
            user.setTime(time);
            System.out.println(user.toString());
            System.out.println(user.getTime());
        } catch (JSONException ex) {
            ex.printStackTrace();
        }
    }

}
